package algorithm.stack;

import algorithm.array.LinkedList;

/**
 * @author bingo
 * @Description   使用链表实现栈
 * @Date 2018/6/4
 */
public class LinkedListStack<E>  implements Stack<E>{

    private LinkedList<E> linkedList;

    public LinkedListStack() {

        linkedList = new LinkedList();
    }

    @Override
    public void push(E e) {
        linkedList.addLast(e);
    }

    @Override
    public E pop() {
        return linkedList.removeLast();
    }

    @Override
    public E peek() {
        return linkedList.getFirst();
    }

    @Override
    public int getSize() {
        return linkedList.getSize();
    }

    @Override
    public boolean isEmpty() {
        return linkedList.getSize()==0;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("LinkedListStack:");
        stringBuilder.append(linkedList.toString());
        return  stringBuilder.toString();
    }

    public static void main(String[] args) {
        LinkedListStack linkedListStack = new LinkedListStack();
        for(int i=0;i<5;i++){
            linkedListStack.push(i);
            System.out.println(linkedListStack);
        }

        linkedListStack.pop();
        System.out.println(linkedListStack);
    }
}
